/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import core.Work;
import core.TestData;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateless;

/**
 *
 * @author Dominik
 */
@Stateless
public class WorkDAO implements IWork {

  @Override
  public void createWork(Work work) {
    if (work == null) {
      throw new RuntimeException("null paprametr");
    } else {
      Integer userId = this.getMaxWorkId();
      work.setId(userId);
      TestData.WORKS_TABLE.put(userId, work);
    }
  }

  @Override
  public void updateWork(Work work) {
    TestData.WORKS_TABLE.put(work.getId(), work);
  }

  @Override
  public void deleteWork(Work work) {
    if (work == null) {
      throw new RuntimeException("null");
    }
    TestData.WORKS_TABLE.remove(work.getId());
  }

  @Override
  public Work findWork(int workId) {
    return TestData.WORKS_TABLE.get(workId);
  }

  @Override
  public Work findWork(String username) {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  @Override
  public List<Work> findAllWorks() {
    return new ArrayList<Work>(TestData.WORKS_TABLE.values());
  }

  private Integer getMaxWorkId() {
    Set<Integer> keys = TestData.WORKS_TABLE.keySet();
    Integer maxId = 1;
    for (Integer key : keys) {
      if (key > maxId) {
        maxId = key;
      }
    }
    return maxId;
  }
}
